using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using QuanLySim.DTO;

namespace QuanLySim.DAO
{
    class clsMangDAO
    {
        private clsCommand m_Command;
        private string m_Erorr;

        public string ERORR
        {
            get { return m_Erorr; }
        }

        public clsMangDAO()
        {
            m_Command = new clsCommand();
        }

        public bool Insert(clsMangDTO MangDto)
        {
            if (MangDto.BMANGMOI == true)//ten mang chua ton tai
            {
                m_Command = new clsCommand();
                string strInsert = "INSERT INTO LOAIMANG (TENMANG, MOTA) VALUES (";
                strInsert += "'" + MangDto.TENMANG + "',";
                strInsert += "'" + MangDto.MOTA + "')";

                //
                if (m_Command.ExecuteNonQuery(strInsert))
                {
                    m_Command = new clsCommand();
                    strInsert = "INSERT INTO DAUSO (DAUSO, TENMANG, LOAISIM) VALUES (";
                    strInsert += "'" + MangDto.DAUSO + "',";
                    strInsert += "'" + MangDto.TENMANG + "',";
                    strInsert += "'" + MangDto.LOAISIM + "')";
                    return m_Command.ExecuteNonQuery(strInsert);
                }
            }
            else//ten mang da ton tai roi
            {
                m_Command = new clsCommand();
                string strInsert = "INSERT INTO DAUSO (DAUSO, TENMANG) VALUES (";
                strInsert += "'" + MangDto.DAUSO + "',";
                strInsert += "'" + MangDto.TENMANG + "')";
                return m_Command.ExecuteNonQuery(strInsert);
            }
            return false;
        }

        public bool DeleteDauSo(clsMangDTO MangDto)
        {
            m_Command = new clsCommand();
            string strDelete = "DELETE FROM DAUSO WHERE DAUSO = ";
            strDelete += "'" + MangDto.DAUSO + "'";

            return m_Command.ExecuteNonQuery(strDelete);
        }

        public bool DeleteToanDauSo(clsMangDTO MangDto)
        {
            m_Command = new clsCommand();
            string strDelete = "DELETE FROM DAUSO WHERE TENMANG = ";
            strDelete += "'" + MangDto.TENMANG + "'";

            return m_Command.ExecuteNonQuery(strDelete);
        }

        public bool DeleteMang(clsMangDTO MangDto)
        {
            m_Command = new clsCommand();
            string strDelete = "DELETE FROM LOAIMANG WHERE TENMANG = ";
            strDelete += "'" + MangDto.TENMANG + "'";

            if (DeleteToanDauSo(MangDto))
            {
                return m_Command.ExecuteNonQuery(strDelete);
            }
            return false;
        }

        public bool UpdateMang(clsMangDTO MangDto)
        {
            m_Command = new clsCommand();
            string strUpdate = "UPDATE LOAIMANG SET MOTA = '" + MangDto.MOTA + "'";
            strUpdate += " WHERE TENMANG = '" + MangDto.TENMANG + "'";

            return m_Command.ExecuteNonQuery(strUpdate);
        }

        public DataTable LoaiMang()
        {
            m_Command = new clsCommand();
            DataTable table = new DataTable();
            string strSelect = "SELECT * FROM LOAIMANG";
            if (m_Command.Connect())
            {
                table = m_Command.Select(strSelect);
                m_Command.CloseConnect();
                return table;
            }
            m_Erorr = m_Command.ERORR;
            return null;
        }

        public DataTable DauSo(clsMangDTO MangDto)
        {
            m_Command = new clsCommand();
            DataTable table = new DataTable();
            string strSelect = "SELECT DAUSO FROM DAUSO WHERE TENMANG = ";
            strSelect += "'" + MangDto.TENMANG + "'";
            if (m_Command.Connect())
            {
                table = m_Command.Select(strSelect);
                m_Command.CloseConnect();
                return table;
            }
            m_Erorr = m_Command.ERORR;
            return null;
        }
    }
}
